package com.itzxx.huazhun.ui.record

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.data.model.RecordChildBean
import com.itzxx.huazhun.databinding.FragmentRecordBinding
import com.itzxx.huazhun.ui.ViewPagerFragmentStateAdapter
import com.itzxx.huazhun.ui.collection.CollectionFragment
import com.itzxx.huazhun.ui.record.detail.RecordDetailActivity
import com.itzxx.huazhun.ui.record.local.RecordLocalFragment
import com.itzxx.huazhun.ui.record.uploaded.RecordUploadedFragment
import com.itzxx.huazhun.ui.testing.TestingFragment
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.fragment.BaseVmDbFragment
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

class RecordFragment : BaseVmDbFragment<RecordViewModel, FragmentRecordBinding>() {


    override fun initView(savedInstanceState: Bundle?) {
        val viewPagerFragmentStateAdapter = ViewPagerFragmentStateAdapter(childFragmentManager, lifecycle)
        viewPagerFragmentStateAdapter.addFragment(RecordUploadedFragment())
        viewPagerFragmentStateAdapter.addFragment(RecordLocalFragment())
        mDatabind.viewpager2.offscreenPageLimit = 2
        mDatabind.viewpager2.adapter = viewPagerFragmentStateAdapter
        mDatabind.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mDatabind.tabOperation.onPageSelected(position)
            }
        })
        mDatabind.tabOperation.observeIndexChange { fromIndex, toIndex, reselect, fromUser ->
            mDatabind.viewpager2.setCurrentItem(toIndex,false)
        }
//        mDatabind.viewpager2.isUserInputEnabled = false
        mDatabind.refreshLayout.setOnRefreshListener {
            val dataList = viewPagerFragmentStateAdapter.getDataList()
            (dataList[mDatabind.viewpager2.currentItem] as BaseVmDbFragment<*, *>).lazyLoadData()
        }
    }



    override fun lazyLoadData() {

    }

    override fun createObserver() {
        mViewModel.run {

        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    fun finishRefresh(){
        mDatabind.refreshLayout.finishRefresh(1000)
    }
}